package com.rockcent.wechat.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.PageViewed;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.dto.CustomerDto;
import com.rockcent.mall.lib.repository.ChannelRepository;
import com.rockcent.mall.lib.repository.MallRepository;
import com.rockcent.mall.lib.repository.MerchantProductRepository;
import com.rockcent.mall.lib.repository.PageViewedRepository;
import com.rockcent.wechat.Routes;
import com.rockcent.wechat.helper.CustomerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by zhangjh on 2016/10/27.
 */
@RestController
@RequestMapping(value = Routes.API_PREFIX)
public class EventController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CustomerController.class);
    @Autowired
    private CustomerHelper customerHelper;
    @Autowired
    private PageViewedRepository pageViewedRepository;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private MerchantProductRepository merchantProductRepository;
    @Autowired
    private ChannelRepository channelRepository;
    /**
     * @api {get} /api/v2/event/Page_viewed 记录用户访问页面的痕迹
     * @apiGroup event
     * @apiVersion 1.0.0
     *
     * @apiHeader {String} domain 对应平台C端域名
     *
     * @apiParam {Long} mall 商城ID
     * @apiParam {String} pageName 访问页面名字
     * @apiParam {String} pageUrl 访问页面Url
     * @apiParam {String} visitip 访问者IP
     * @apiParam {Long} [channelId] 渠道id
     * @apiParam {Long} [productId] 产品id
     * @apiParam {String} [fromTerminal] 终端类型 WECHAT , IOS  , ANDROID , BROWER, MERCHANT
     *
     * @apiSuccess {String} id ID
     * @apiSuccess {Integer} dateCreated 创建时间
     * @apiSuccess {Integer} lastUpdated 更新时间
     * @apiSuccess {Integer} version 版本
     * @apiSuccess {String} channelId 渠道ID
     * @apiSuccess {Long} mallId 平台ID
     * @apiSuccess {String} pageName 页面名称
     * @apiSuccess {String} pageUrl 页面URL
     * @apiSuccess {String} visitip 访问者IP
     * @apiSuccess {Long} merchantId 商铺ID
     * @apiSuccess {Long} productId 产品ID
     * @apiSuccess {String} productType 产品类型
     * @apiSuccess {String} fromTerminal 终端类型 WECHAT , IOS  , ANDROID , BROWER, MERCHANT
     *
     * @apiSuccessExample {json} Success-Response:
     *{
     *  "code": "1",
     *  "desc": "成功",
     *  "data": {
     *              "id": 72115,
     *              "dateCreated": "2016-10-27 16:53:27",
     *              "version": 0,
     *              "mallId": 1,
     *              "pageName": "hehehehehehe",
     *              "pageUrl": "www.mdjj.com",
     *              "visitip": "129.168.1.1",
     *              "merchantId": 2,
     *              "productId": 1,
     *              "productType": "GOODS",
     *              "fromTerminal": "WECHAT"
     *          }
     *  }
     */
    @RequestMapping(value = Routes.EVENT_PAGE_VIEWED, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult checkProductLimit(PageViewed pageViewed, HttpServletRequest request) {

        CustomerDto customerDto = customerHelper.getSession(request);
        RestDoing doing = jr ->{
            if (pageViewed.getPageName()==null){
                jr.remind("页面名称不能为空",log);
                return;
            }
            if (pageViewed.getPageUrl()==null){
                jr.remind("页面地址不能为空",log);
                return;
            }
            if (pageViewed.getVisitip()==null){
                jr.remind("访问者地址不能为空",log);
                return;
            }
            pageViewed.setMallId(customerDto.getMallId());
            pageViewed.setDateCreated(new Date());
            if (pageViewed.getProductId()!=null){
                Product product = merchantProductRepository.findOne(pageViewed.getProductId());
                if (product==null){
                    jr.errorParam("商品不存在",log);
                    return;
                }
                pageViewed.setMerchantId(product.getMerchantId());
                pageViewed.setProductId(product.getId());
                pageViewed.setProductType(product.getType());
            }
            if (pageViewed.getChannelId()!=null && channelRepository.exists(Long.parseLong(pageViewed.getChannelId()))){
                jr.errorParam("该渠道不存在",log);
                return;
            }
            PageViewed savePageView = pageViewedRepository.save(pageViewed);
            if (savePageView == null){
                String errorLog = "pageViewed :" + pageViewed.getPageName()+" url:" +pageViewed.getPageUrl()+
                        " ip:"+pageViewed.getVisitip() + " record failed!!";
                log.error(errorLog);
                jr.setData(false);
            }
            jr.setData(savePageView);
        };
        return doing.go(request, log);
    }
}
